//
//  XDPhotoTextCollectionViewCell.m
//  Le way walkers
//
//  Created by POP-mac on 15/5/14.
//  Copyright (c) 2015年 LTXZ. All rights reserved.
//

#import "XDPhotoTextCollectionViewCell.h"
#import "XDShakeViewController.h"
#import "UIImageView+WebCache.h"

@interface XDPhotoTextCollectionViewCell ()
@property (nonatomic, assign) BOOL suibian;
@property (nonatomic, retain) UILabel *text;
@property (nonatomic, retain) UILabel *place;
@property (nonatomic, assign) CGPoint op;
@property (nonatomic, retain) UIImageView *time;
@property (nonatomic, assign) CGFloat fff;
@end

@implementation XDPhotoTextCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.imageView = [[[UIImageView alloc] init] autorelease];
//        [self.imageView setBackgroundColor:[UIColor yellowColor]];
        self.imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)] autorelease];
        
        [self.imageView addGestureRecognizer:tap];
        
        [self.contentView addSubview:self.imageView];
        
        self.scroll = [[UIScrollView alloc] init];
        self.scroll.contentSize = CGSizeMake(self.contentView.frame.size.width - 30, 500);
        self.scroll.contentOffset = CGPointMake(0, 0);
//        [self.scroll setBackgroundColor:[UIColor whiteColor]];
        [self.scroll setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]];
        [self.contentView addSubview:self.scroll];
        
        self.text = [[[UILabel alloc] init] autorelease];
        [self.text setFont:[UIFont systemFontOfSize:15]];
        [self.text setNumberOfLines:0];
        [self.text setTextColor:[UIColor whiteColor]];
//        [self.text setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.2]];
        [self.scroll addSubview:self.text];
        
        
        self.place = [[[UILabel alloc] init] autorelease];
        [self.place setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]];
        [self.place setTextColor:[UIColor whiteColor]];
//        self.place.alpha = 0;
        [self.place setFont:[UIFont systemFontOfSize:14]];
        [self.contentView addSubview:self.place];
        
        self.time = [[[UIImageView alloc] init] autorelease];
//        [self.time setBackgroundColor:[UIColor greenColor]];
        self.time.image = [UIImage imageNamed:@"clock_gray"];
        [self.place addSubview:self.time];
        
        
    }
    return self;
}

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    self.suibian = !self.suibian;
    if (self.suibian) {
//        [UIView animateWithDuration:0.6 animations:^{
//            self.scroll.alpha = 0;
//            self.scroll.center = CGPointMake(self.contentView.frame.size.width / 2.0, 700);
//        }];
        [UIView animateWithDuration:0.5 animations:^{
            self.scroll.alpha = 0;
            self.scroll.center = CGPointMake(self.contentView.frame.size.width / 2.0, 700);
        } completion:^(BOOL finished) {
            self.scroll.hidden = YES;
        }];
    }
    else
    {
//        [UIView animateWithDuration:0.6 animations:^{
//            self.scroll.alpha = 0.8;
//            self.scroll.center = CGPointMake(self.contentView.frame.size.width / 2.0, self.op.y);
//        }];
        self.scroll.hidden = NO;
        [UIView animateWithDuration:0.5 animations:^{
            self.scroll.alpha = 1;
            if (self.fff <= 110) {
                self.scroll.frame = CGRectMake(0, self.contentView.frame.size.height - self.fff - 40, self.contentView.frame.size.width,self.fff + 5);
            }
            else
            {
                self.scroll.frame = CGRectMake(0, self.contentView.frame.size.height - 150, self.contentView.frame.size.width, 110);
            }
            self.scroll.contentSize = CGSizeMake(self.frame.size.width - 30, self.fff + 5);
        } completion:^(BOOL finished) {
            //
        }];
    }
}


- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [self.scroll setFrame:CGRectMake(0, self.contentView.frame.size.height - 150, self.contentView.frame.size.width, 110)];
    [self.imageView setFrame:CGRectMake(0, self.contentView.frame.size.height / 3.0 - 100, self.contentView.frame.size.width, 300)];
    self.op = CGPointMake(self.scroll.center.x, self.scroll.center.y);
//    [self.text setFrame:CGRectMake(0, self.contentView.frame.size.height / 3.0 + 200, self.contentView.frame.size.width, 130)];
    [self.text setFrame:CGRectMake(15, 5, self.contentView.frame.size.width - 30, 500)];
    [self.place setFrame:CGRectMake(0, self.contentView.frame.size.height - 35, self.contentView.frame.size.width, 25)];
    [self.time setFrame:CGRectMake(15, 5, 15, 15)];
}

- (void)setModel:(XDPhotoText *)model
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
//    self.text.text = @"中国教育永远不缺少借口。最初说港台流行文化影响了孩子。后来说日本漫画和电子游戏影响了孩子。后来说网络和电脑游戏影响了孩子。现在又说手机影响了孩子。就好像中国教育从来都没错过一样!,中国教育永远不缺少借口。最初说港台流行文化影响了孩子。后来说日本漫画和电子游戏影响了孩子。后来说网络和电脑游戏影响了孩子。现在又说手机影响了孩子。就好像中国教育从来都没错过一样!,中国教育永远不缺少借口。最初说港台流行文化影响了孩子。后来说日本漫画和电子游戏影响了孩子。后来说网络和电脑游戏影响了孩子。现在又说手机影响了孩子。就好像中国教育从来都没错过一样!,中国教育永远不缺少借口。最初说港台流行文化影响了孩子。后来说日本漫画和电子游戏影响了孩子。后来说网络和电脑游戏影响了孩子。现在又说手机影响了孩子。就好像中国教育从来都没错过一样!,中国教育永远不缺少借口。最初说港台流行文化影响了孩子。后来说日本漫画和电子游戏影响了孩子。后来说网络和电脑游戏影响了孩子。现在又说手机影响了孩子。就好像中国教育从来都没错过一样!";
//    self.place.text = @"大连 沙河口区 数码广场  ⏰2015.5.15";
    self.text.text = model.text;
    self.fff = [[self class] hightWithText:model.text];
    if (self.fff <= 110) {
        self.scroll.frame = CGRectMake(0, self.contentView.frame.size.height - self.fff - 40, self.contentView.frame.size.width,self.fff + 5);
    }
    self.scroll.contentSize = CGSizeMake(self.frame.size.width - 30, self.fff + 5);
    [self.text setFrame:CGRectMake(15, 5, self.contentView.frame.size.width - 30, self.fff)];
    if (model.photo_info.allKeys.count >= 1) {
        NSLog(@"laile");
        CGFloat xx = [[model.photo_info objectForKey:@"w"] integerValue];
        CGFloat yy = [[model.photo_info objectForKey:@"h"] integerValue];
        [self.imageView setFrame:CGRectMake(0, self.frame.size.height / 2.0 - (self.frame.size.width / xx * yy / 2), self.contentView.frame.size.width, self.frame.size.width / xx * yy)];
    }
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"69E58PIChJC_1024.jpg"]];
    self.place.text = [NSString stringWithFormat:@"         %@", model.local_time];
}

+ (CGFloat)hightWithText:(NSString *)text
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
    CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width - 30, 8000);
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}



























@end
